$(document).ready(function() {
    $.get("../session/", function(data){
       
        if(data=="1"){
            $("#dialog").dialog({
                bgiframe: true,
                autoOpen: false,
                height: 220,
                modal: true,
                buttons: {   
                },
                close: function() {
                    allFields.val('').removeClass('ui-state-error');
                }
            });
        }else{
            $("#dialog").dialog({
                bgiframe: true,
                autoOpen: true,
                height: 220,
                modal: true,
                buttons: {
                    '登录': function() {
                        $('#name').removeClass('ui-state-error');
                        $('#password').removeClass('ui-state-error');
                        var bValid = true;
                        bValid = bValid && checkLength($('#name'),"username",3,16);
                        bValid = bValid && checkLength($('#password'),"password",5,16);
                        bValid = bValid && checkRegexp($('#name'),/^[a-z]([0-9a-z_])+$/i,"Username may consist of a-z, 0-9, underscores, begin with a letter.");
                        bValid = bValid && checkRegexp($('#password'),/^([0-9a-zA-Z])+$/,"Password field only allow : a-z 0-9");
                        if (bValid) {
                            $.post('../login/',{
                                name:$('#name').val(),
                                psw:$('#password').val(),
                                act:1
                            }, function(data){
                                if(data=="1"){
                                    $('#dialog').dialog('close');
                                }else{
                                    updateTips("用户名或者密码错误，请核对。");
                                }
                            })
                        }
                    }
                },
                close: function() {
                    allFields.val('').removeClass('ui-state-error');
                }
            });
        }
    });

////////////////
//$("#tabs").tabs();
//$("#tabs2").tabs();
          
// $(".demos-list").selectable();
    
});

 
function showid(){
    $(".ui-selected").each(
        function(i){
            alert($(this).attr("id"));
        }
        );
}

function checkLength(o,n,min,max) {

    if ( o.val().length > max || o.val().length < min ) {
        updateTips("Length of " + n + " must be between "+min+" and "+max+".");
        o.addClass('ui-state-error');
        return false;
    } else {
        return true;
    }

}

function checkRegexp(o,regexp,n) {

    if ( !( regexp.test( o.val() ) ) ) {
        updateTips(n);
        o.addClass('ui-state-error');
        return false;
    } else {
        return true;
    }
}
function updateTips(t) {
    $("#validateTips").text(t).effect("highlight",{},1500);
}
 